package autoteam.ui.dialogs;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import org.jasypt.util.password.BasicPasswordEncryptor;

import autoteam.tables.AutoTeamTableModel;
import db.ColumnData;
import db.DataAccessObject;

public class UserCreator extends AutoTeamDialog
{
    private static final long serialVersionUID = 3353525672660687802L;
    
    private JButton addButton = new JButton( "Add" );
    private JButton closeButton = new JButton( "Close" );
    
    private AutoTeamTableModel model;
    
    private JTextField firstNameField;
    private JTextField lastNameField;
    private JTextField userNameField;
    //private JPasswordField currentPasswordField;
    private JPasswordField newPasswordField;
    private JPasswordField rePasswordField;
    
    private JComboBox roleBox;
    
    private BasicPasswordEncryptor passwordEncryptor = new BasicPasswordEncryptor();
    
    public UserCreator( JFrame dialogOwner )
    {
        super( dialogOwner );
        this.setTitle( "Edit User" );
        this.setDialogSize( new Dimension( 300, 250 ) );
        
        JPanel buttonPane = new JPanel( );
        buttonPane.add( addButton );
        buttonPane.add( closeButton );
        setButtonActions( this );
        
        JPanel formPane = new JPanel( new GridLayout( 6, 2 ) );
        JLabel firstName = new JLabel( "First Name: " );
        JLabel lastName = new JLabel( "Last Name: " );
        JLabel userName = new JLabel( "Username: " );
        //JLabel currentPassword = new JLabel("Current Password: ");
        JLabel newPassword = new JLabel( "New Password: " );
        JLabel rePassword = new JLabel( "Retype Password: " );
        JLabel roleLabel = new JLabel( "Role: ");
        
        firstNameField = new JTextField( 25 );
        lastNameField = new JTextField( 25 );
        userNameField = new JTextField( 25 );
        //currentPasswordField = new JPasswordField(25);
        newPasswordField = new JPasswordField( 25 );
        rePasswordField = new JPasswordField( 25 );
        
        String[] roleArray = {"Admin", "Athlete", "Fan"};
        
        roleBox = new JComboBox(roleArray);
        
        formPane.add( firstName );
        formPane.add( firstNameField );
        
        formPane.add( lastName );
        formPane.add( lastNameField );
        
        formPane.add( userName );
        formPane.add( userNameField );
        
        //formPane.add(currentPassword);
        //formPane.add(currentPasswordField);
        
        formPane.add( roleLabel );
        formPane.add( roleBox );
        
        formPane.add( newPassword );
        formPane.add( newPasswordField );
        
        formPane.add( rePassword );
        formPane.add( rePasswordField );
        
        Container contentPane = getContentPane( );
        contentPane.add( formPane, BorderLayout.CENTER );
        contentPane.add( buttonPane, BorderLayout.SOUTH );
        
        pack( );
    }
    
    public UserCreator( JFrame dialogOwner, AutoTeamTableModel model )
    {
        this( dialogOwner );
        this.model = model;
    }
    
    @ Override
    protected void setButtonActions( final AutoTeamDialog thisDialog )
    {
        addButton.addActionListener( new ActionListener( )
        {
            @ Override
            public void actionPerformed( ActionEvent e )
            {
                if ( !newPasswordField.getText( ).equals( rePasswordField.getText( ) ) )
                {
                    JOptionPane.showMessageDialog
                    ( 
                         new JFrame( ), "The password fields must match", "Error",
                         JOptionPane.ERROR_MESSAGE 
                    );
                    
                    return;
                }
                
                // TODO
                DataAccessObject dao = new DataAccessObject( );
                dao.addRecord
                ( 
                     "users", new ColumnData( "username", userNameField.getText( ) ),
                     new ColumnData( "firstname", firstNameField.getText( ) ),
                     new ColumnData( "lastname", lastNameField.getText( ) ), 
                     new ColumnData( "sport", "default" ),
                     new ColumnData( "role", roleBox.getSelectedIndex()+1 ), 
                     new ColumnData( "password", passwordEncryptor.encryptPassword(newPasswordField.getText()) )
                );
                
                model.add( 0 );
                thisDialog.close( );
                
            }
        } );
        
        closeButton.addActionListener( new ActionListener( )
        {
            @ Override
            public void actionPerformed( ActionEvent e )
            {
                thisDialog.close( );
            }
        } );
    }
}
